package com.probox.common.entity.service.platfom.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.platfom.PlatformSendSmsMapper;
import com.probox.common.entity.domain.platfom.PlatformSendSms;
import com.probox.common.entity.service.platfom.IPlatformSendSmsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 短信发送记录Service业务层处理
 *
 *
 * @date 2021-06-22
 */
@Service
@Transactional
public class PlatformSendSmsServiceImpl extends ServiceImpl<PlatformSendSmsMapper, PlatformSendSms> implements IPlatformSendSmsService {
    @Autowired
    private PlatformSendSmsMapper platformSendSmsMapper;


    /**
     * 查询短信发送记录
     *
     * @param sendId 短信发送记录ID
     * @return 短信发送记录
     */
    @Override
    public PlatformSendSms selectPlatformSendSmsById(Long sendId) {
        return this.getById(sendId);
    }

    /**
     * 查询短信发送记录列表
     *
     * @param platformSendSms 短信发送记录
     * @return 短信发送记录
     */
    @Override
    public List<PlatformSendSms> queryList(PlatformSendSms platformSendSms) {
        Long platformId = SecurityUtils.getPlatformId();
        if(platformId == null){
            throw new BaseException("当前账号异常");
        }
        platformSendSms.setPlatformId(platformId);
        QueryWrapper<PlatformSendSms> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(platformSendSms.getPlatformId())) && platformSendSms.getPlatformId() != null) {
            queryWrapper.eq("platform_id", platformSendSms.getPlatformId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformSendSms.getMobile())) && platformSendSms.getMobile() != null) {
            queryWrapper.like("mobile", platformSendSms.getMobile());
        }
        if (ObjectUtil.isNotEmpty(platformSendSms.getValidateDate())) {
            queryWrapper.ge("validate_date", platformSendSms.getValidateDate());
        }
        if (ObjectUtil.isNotEmpty(platformSendSms.getExpireDate())) {
            queryWrapper.le("expire_date", platformSendSms.getExpireDate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformSendSms.getState())) && platformSendSms.getState() != null) {
            queryWrapper.eq("state", platformSendSms.getState());
        }
        queryWrapper.orderByDesc("SEND_DATE");
        return platformSendSmsMapper.selectList(queryWrapper);
    }

    /**
     * 新增短信发送记录
     *
     * @param platformSendSms 短信发送记录
     * @return 结果
     */
    @Override
    public boolean insertPlatformSendSms(PlatformSendSms platformSendSms) {

        return this.save(platformSendSms);

    }

    /**
     * 修改短信发送记录
     *
     * @param platformSendSms 短信发送记录
     * @return 结果
     */
    @Override
    public boolean updatePlatformSendSms(PlatformSendSms platformSendSms) {

        return this.updateById(platformSendSms);
    }

    /**
     * 批量删除短信发送记录
     *
     * @param sendIds 需要删除的短信发送记录ID
     * @return 结果
     */
    @Override
    public boolean deletePlatformSendSmsByIds(Long[] sendIds) {
        return this.removeByIds(Arrays.asList(sendIds));
    }


    @Override
    public int validate(Long platformId, String mobile, String code) {
        QueryWrapper<PlatformSendSms> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("platform_id", platformId);
        queryWrapper.eq("mobile", mobile);
        queryWrapper.eq("sms_code", code);
        queryWrapper.eq("state", DataStatus.invalid.getKey());
        List<PlatformSendSms> list = platformSendSmsMapper.selectList(queryWrapper);
        if (list.size()>0) {
            PlatformSendSms sms = list.get(0);
            sms.setState(DataStatus.valid.getKey());
            sms.setValidateCode(code);
            sms.setValidateDate(new Date());
            return platformSendSmsMapper.updateById(sms);
        }
        return 0;
    }
}
